package cn.edu.hcnu.servlet;

import cn.edu.hcnu.model.EmpDao;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String uri=request.getRequestURI();
        System.out.println(uri);
        uri=uri.substring(1);

        if("login".equals(uri)){
            doLogin(request,response);
        }else  if(uri.equals("exit")){
            HttpSession session=request.getSession();
            session.invalidate();
            response.sendRedirect("login.html");

        }

    }
    public void doLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {



        String id = request.getParameter("username");
        String passwd = request.getParameter("password");
        System.out.println("id:" + id);
        System.out.println("password:" + passwd);


        String username="scott";
        String password="tiger";
        String url="jdbc:oracle:thin:@localhost:1521:orcl";
        Connection conn=null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        String sql="SELECT * FROM t_users WHERE user_id=? AND  password=?";

        try {
            //加载驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            //创建连接
            conn= DriverManager.getConnection(url,username,password);
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,id);
                pstmt.setString(2,passwd);
                rs=pstmt.executeQuery();
                if(rs.next()){
                    System.out.println("登录成功");
                    HttpSession session=request.getSession();
                    session.setAttribute("username",id);
                    response.sendRedirect("index.jsp");

                    //response.sendRedirect("viewEmp");

                    /*
                    //请求转发
                    //request.getRequestDispatcher("viewEmp.jsp").forward(request,response);
                    //response.sendRedirect("Emp.jsp");
                    //sql="SELECT * FROM  emp";
                    sql="select * from (select rownum rn,t.* from (select * from emp) t where rownum<=? ) where rn>=?";

                    pstmt=conn.prepareStatement(sql);


                    QueryRunner queryRunner=new QueryRunner();
                    List emplist=queryRunner.query(conn,sql,new BeanListHandler<EmpDao>(EmpDao.class),endRow,startRow);*/
                    /*pstmt.setInt(1,endRow);
                    pstmt.setInt(2,startRow);
                    rs=pstmt.executeQuery();
                    List emplist=new ArrayList();
                    while (rs.next()){
                        EmpDao emp=new EmpDao();
                        emp.setEmpno(rs.getInt("EMPNO"));
                        emp.setEname(rs.getString("ENAME"));
                        emp.setJob(rs.getString("JOB"));
                        emp.setMgr(rs.getInt("MGR"));
                        emp.setHiredate(rs.getDate("HIREDATE"));
                        emp.setSal(rs.getFloat("SAL"));
                        emp.setComm(rs.getFloat("COMM"));
                        emp.setDeptno(rs.getInt("DEPTNO"));
                        emplist.add(emp);
                    }
                    HttpSession session=request.getSession();
                    request.setAttribute("empData",emplist);
                    session.setAttribute("username",id);
                //                request.getRequestDispatcher("emp.html").forward(request,response);
                    //跳转jsp前先查询总数
                    sql="select  count(*) from emp";
                    pstmt=conn.prepareStatement(sql);
                    rs=pstmt.executeQuery();
                    rs.next();
                    int rowCount=rs.getInt(1);
                    int totalPage=rowCount%pageSize==0?(rowCount/pageSize):(rowCount/pageSize)+1;

                    request.setAttribute("totalPage",totalPage);
               request.getRequestDispatcher("index.jsp").forward(request,response);
               */
            }
            else{
                response.sendRedirect("login.html");
            }
            System.out.println(conn);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            try {
                rs.close();
                pstmt.close();
                conn.close();

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
